Decoding Base64

Hello,

I have been playing with "toBase64_" and "fromBase64_" and notice some weirdness...
It encodes fine, but when I decode it, I get am returned a string which seems to be "on top of" the encoded version.
It has the encoded characters at the end of the decoded string.
But if I get the length, it returns the length of the correctly decocded string, not what is printed!
So I can ues that to truncate and correct the decode...

I guess the decode algorithm duplicates the encoded version and then decodes it in place but fails to null the remaining base64 characters.
I don't see how the length function returns a number different from the string. I would think it counts up to the first null character.

I am running DOORS 9.3.0.3.
Is it the just this version?
Can anyone explain what is going on?
 

string Original = "Hello World"
print "Original [" Original "]\n"
 
// Encode it
string Encoded
toBase64_(Original, Encoded)
print "Encoded [" Encoded "]\n"
 
// Decoded it
string Decoded
fromBase64_(Encoded, Decoded) 
print "Decoded [" Decoded "]\n"
 
// Decoded length
int DecodedLength = length(Original)
print "DecodedLength : " DecodedLength "\n"
 
// Fix using the Decoded length
string Fixed = Decoded[0:DecodedLength-1]
print "Fixed [" Fixed "]\n"

 


Output:

 

 

Original [Hello World]
Encoded [SGVsbG8gV29ybGQ=
]
Decoded [Hello WorldybGQ=
]
DecodedLength : 11
Fixed [Hello World]

 


SystemAdmin - Mon Apr 30 04:32:45 EDT 2012

Re: Decoding Base64
SystemAdmin - Mon Apr 30 05:55:22 EDT 2012

Doh! I screwed up. I'm getting the length of the original, not the decoded!
Using "length(Decoded)" returns the correct length of the poorly decoded string.
 

int DecodedLength = length(Original) //oops

 


So to correctly decode I will need to know the length of the Original as well...

 

Re: Decoding Base64
llandale - Mon Apr 30 11:03:24 EDT 2012

These functions seems to be bugged since they do not return the same results each time. Run the following over and over:

string    NoEOL(string InString)
{     int     Len = length(InString)
        if (InString[Len-1] == '\n') //
        then return(InString[0:Len-2] "@")
        else return(InString)
}
 
 
void    fDecodeBullCrap(string Encoded, &Decoded)
{     int i, LenMin = -1, Len
        string  DecodedTemp = ""
        //fromBase64_(Encoded Encoded, DecodedTemp)
        for (i=0; i<100; i++)
        {  fromBase64_(DecodedTemp, DecodedTemp)
           //toBase64_  (Encoded, DecodedTemp)
           fromBase64_(Encoded, DecodedTemp)
           Len = length(DecodedTemp)
           if (LenMin == -1 or Len < LenMin) LenMin = Len
           //print "\t" Len "\t" LenMin "\t[" (NoEOL(DecodedTemp)) "]\n"
        }
        Decoded = DecodedTemp[0:LenMin-1]
}
 
void    DoTest(string Original)
{     string  Encoded = "", Decoded = ""
        toBase64_(Original, Encoded) 
        fDecodeBullCrap(Encoded, Decoded)
 
        bool    Success = Original == Decoded
        print Success "\t[" Original "]\t^" (NoEOL(Encoded)) "^\t[" (NoEOL(Decoded)) "]\n"
}
 
DoTest("Hello Word")   // Not "Hallo"
DoTest("AA")
DoTest("AAx d")


-Louie

Re: Decoding Base64
SystemAdmin - Mon Apr 30 11:40:06 EDT 2012

llandale - Mon Apr 30 11:03:24 EDT 2012

These functions seems to be bugged since they do not return the same results each time. Run the following over and over:

string    NoEOL(string InString)
{     int     Len = length(InString)
        if (InString[Len-1] == '\n') //
        then return(InString[0:Len-2] "@")
        else return(InString)
}
 
 
void    fDecodeBullCrap(string Encoded, &Decoded)
{     int i, LenMin = -1, Len
        string  DecodedTemp = ""
        //fromBase64_(Encoded Encoded, DecodedTemp)
        for (i=0; i<100; i++)
        {  fromBase64_(DecodedTemp, DecodedTemp)
           //toBase64_  (Encoded, DecodedTemp)
           fromBase64_(Encoded, DecodedTemp)
           Len = length(DecodedTemp)
           if (LenMin == -1 or Len < LenMin) LenMin = Len
           //print "\t" Len "\t" LenMin "\t[" (NoEOL(DecodedTemp)) "]\n"
        }
        Decoded = DecodedTemp[0:LenMin-1]
}
 
void    DoTest(string Original)
{     string  Encoded = "", Decoded = ""
        toBase64_(Original, Encoded) 
        fDecodeBullCrap(Encoded, Decoded)
 
        bool    Success = Original == Decoded
        print Success "\t[" Original "]\t^" (NoEOL(Encoded)) "^\t[" (NoEOL(Decoded)) "]\n"
}
 
DoTest("Hello Word")   // Not "Hallo"
DoTest("AA")
DoTest("AAx d")


-Louie

Yep, inconsistant results.

Well, I suppose the functions are undocumented. I just wonder about the competance of the developers if a function like this gets into released software. Do they not do unit tests of their functions?? It took me one try to realise it dosen't work. This is something that should be really easy to implement and test. And if it fails, don't release it!

I hope it isn't being used internally... I wonder if it is used with OLEs and causes the sporadic corruption.

Re: Decoding Base64
Mathias Mamsch - Tue May 08 11:58:03 EDT 2012

SystemAdmin - Mon Apr 30 11:40:06 EDT 2012
Yep, inconsistant results.

Well, I suppose the functions are undocumented. I just wonder about the competance of the developers if a function like this gets into released software. Do they not do unit tests of their functions?? It took me one try to realise it dosen't work. This is something that should be really easy to implement and test. And if it fails, don't release it!

I hope it isn't being used internally... I wonder if it is used with OLEs and causes the sporadic corruption.

What?? Are you saying they broke these functions ... They used to work fine in DOORS 8, your examples work for my test on DOORS 8.2. DOORS 9.3.0 I have the same issues ... sigh ... I hate it when that happens ... Regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Re: Decoding Base64
Mathias Mamsch - Tue May 08 12:04:32 EDT 2012

Mathias Mamsch - Tue May 08 11:58:03 EDT 2012
What?? Are you saying they broke these functions ... They used to work fine in DOORS 8, your examples work for my test on DOORS 8.2. DOORS 9.3.0 I have the same issues ... sigh ... I hate it when that happens ... Regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Oh suuuure .... and when I try the "compressed" base64 functions I get: "PERM toXTFormat is no longer supported!". After breaking the normal base64 functions, why not remove some functionality from DXL... This calls for revenge! Regards, Mathias

Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Re: Decoding Base64
Karray - Wed Sep 04 10:16:00 EDT 2013

Hi,

To export an OLE from Doors to Word, I do as following:

 

I select the OLE and I put it in a file  ( I use: exportPicture(ole,filename , formatPNG))

 

I put the contain of the file into a stream and then into a buffer.( I use:  Stream inStream = read binary inFile) 

 

I use the function  "_BASE64"  to convert the content of the buffer to BASE64 .

 

Then I put the _BASE64 content into the XML script.

 

But it doesn't work..

Can you help me?.

 

Thank you.

////////////////////////////////////

This is my code:

 

void readBinaryFile(string NameFile, Buffer b)
{
Stream x = read (binary (NameFile))
char c
 
while(true)
{
x >> c
 
if (end x)
break
else { b += c}
 
}
close x
}

 

void CheckisOLE (Object obj)

{
EmbeddedOleObject ole
RichText rtf
string s = richTextWithOle obj."Object Text"
string filename
Buffer b = create()

 

for rtf in s do
{
if (rtf.isOle)
{
//Mémorisation des OLE sous forme d'image.
ole = rtf.getEmbeddedOle
string filename = "C:\\essai-rtfloop-.png"
print "Exporting " filename "\n"
exportPicture(ole,filename , formatPNG)
 
 
// Read the image content
readBinaryFile(filename, b)
string outpstr = ""
print "bufferB content is equal to:" b "\n"
outpstr = stringOf(b)

setempty b

print "outpstr content is equal to:" outpstr "\n" "\n" "\n"

 

//Coding the content on base64

string Encoded1 = ""
toBase64_(outpstr, Encoded1)
print "Encoded1 content is equal to:" Encoded1 "\n" "\n" "\n"
 
 
//displaying it on xml.
AddBuffer("<w:pict>")
AddBuffer("<w:binData w:name=\"wordml://03000001.png\" xml:space=\"preserve\">")
AddBuffer(Encoded1)
AddBuffer("</w:binData>")
AddBuffer("<v:shape>")
AddBuffer("<v:imagedata src=\"wordml://03000001.png\" o:title=\"\"/>")
AddBuffer("</v:shape>")
AddBuffer("</w:pict>")
}
 

Re: Decoding Base64
Adamarla - Thu Sep 05 03:47:02 EDT 2013

Karray - Wed Sep 04 10:16:00 EDT 2013

Hi,

To export an OLE from Doors to Word, I do as following:

 

I select the OLE and I put it in a file  ( I use: exportPicture(ole,filename , formatPNG))

 

I put the contain of the file into a stream and then into a buffer.( I use:  Stream inStream = read binary inFile) 

 

I use the function  "_BASE64"  to convert the content of the buffer to BASE64 .

 

Then I put the _BASE64 content into the XML script.

 

But it doesn't work..

Can you help me?.

 

Thank you.

////////////////////////////////////

This is my code:

 

void readBinaryFile(string NameFile, Buffer b)
{
Stream x = read (binary (NameFile))
char c
 
while(true)
{
x >> c
 
if (end x)
break
else { b += c}
 
}
close x
}

 

void CheckisOLE (Object obj)

{
EmbeddedOleObject ole
RichText rtf
string s = richTextWithOle obj."Object Text"
string filename
Buffer b = create()

 

for rtf in s do
{
if (rtf.isOle)
{
//Mémorisation des OLE sous forme d'image.
ole = rtf.getEmbeddedOle
string filename = "C:\\essai-rtfloop-.png"
print "Exporting " filename "\n"
exportPicture(ole,filename , formatPNG)
 
 
// Read the image content
readBinaryFile(filename, b)
string outpstr = ""
print "bufferB content is equal to:" b "\n"
outpstr = stringOf(b)

setempty b

print "outpstr content is equal to:" outpstr "\n" "\n" "\n"

 

//Coding the content on base64

string Encoded1 = ""
toBase64_(outpstr, Encoded1)
print "Encoded1 content is equal to:" Encoded1 "\n" "\n" "\n"
 
 
//displaying it on xml.
AddBuffer("<w:pict>")
AddBuffer("<w:binData w:name=\"wordml://03000001.png\" xml:space=\"preserve\">")
AddBuffer(Encoded1)
AddBuffer("</w:binData>")
AddBuffer("<v:shape>")
AddBuffer("<v:imagedata src=\"wordml://03000001.png\" o:title=\"\"/>")
AddBuffer("</v:shape>")
AddBuffer("</w:pict>")
}
 

The problem is null 'characters' in the binary stream terminating the Buffer.

See this thread.

 

Re: Decoding Base64
Karray - Thu Sep 05 05:19:51 EDT 2013

Adamarla - Thu Sep 05 03:47:02 EDT 2013

The problem is null 'characters' in the binary stream terminating the Buffer.

See this thread.

 

Thanks for your help,
 
Yes, null 'characters' is the problem.
 
In the thread "Read picture data to buffer?'", I see a solution to read and write the same picture.
 
But in my case I want to recuperate the "binary + null 'characters'" and transmit all datas to the function _BASE64.

 This is the part of code where ther are a problem:

// Read the image content
readBinaryFile(filename, b)    ////// IN THIS PART  Buffer b doesn't recuperate the  null 'character
string outpstr = ""
print "bufferB content is equal to:" b "\n"
outpstr = stringOf(b)

setempty b

print "outpstr content is equal to:" outpstr "\n" "\n" "\n"

 

//Coding the content on base64

string Encoded1 = ""
toBase64_(outpstr, Encoded1)   ////// IN THIS PART ENCODING IS INCORRECT
print "Encoded1 content is equal to:" Encoded1 "\n" "\n" "\n"

 

Can you help me?

Re: Decoding Base64
Karray - Fri Sep 06 08:00:38 EDT 2013

Karray - Thu Sep 05 05:19:51 EDT 2013

Thanks for your help,
 
Yes, null 'characters' is the problem.
 
In the thread "Read picture data to buffer?'", I see a solution to read and write the same picture.
 
But in my case I want to recuperate the "binary + null 'characters'" and transmit all datas to the function _BASE64.

 This is the part of code where ther are a problem:

// Read the image content
readBinaryFile(filename, b)    ////// IN THIS PART  Buffer b doesn't recuperate the  null 'character
string outpstr = ""
print "bufferB content is equal to:" b "\n"
outpstr = stringOf(b)

setempty b

print "outpstr content is equal to:" outpstr "\n" "\n" "\n"

 

//Coding the content on base64

string Encoded1 = ""
toBase64_(outpstr, Encoded1)   ////// IN THIS PART ENCODING IS INCORRECT
print "Encoded1 content is equal to:" Encoded1 "\n" "\n" "\n"

 

Can you help me?

Can you help me? I am Blocked !

Thank you.